package com.sqly.doc.user;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.ServicePackageItem;
import com.sqly.doc.pojo.Team;
import com.sqly.doc.util.BinderUtil;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.StringUtils;

import cz.msebera.android.httpclient.Header;

/**
 * 服务项目新增或编辑界面
 * Created by Administrator on 2017/11/18 0018.
 */

public class servicepack_item_add extends MyBaseActivity{
    //标题
    TextView title, uname;
    EditText item_name, his_code,dj, cs;
    ServicePackageItem obj;
    Context con = servicepack_item_add.this;
    String url = HttpUrl.AddSerViceItem;

    @Override
    protected void initUI() {
        setContentView(R.layout.servicepack_item_add);
        init();
    }

    private void init(){
        //标题
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("服务项目管理");
        uname.setText("保存");
        item_name = (EditText) findViewById(R.id.item_name);
        his_code = (EditText) findViewById(R.id.his_code);
        dj = (EditText)findViewById(R.id.dj);
        cs = (EditText)findViewById(R.id.cs);

        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != item_name && StringUtils.isEmpty(item_name.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入服务项目名称");
                }else if(item_name.getText().toString().length()>21){
                    MyToastUtil.ShowToast(servicepack_item_add.this, "服务项目名称最多输入20个字符！");
                }else if(null != his_code && StringUtils.isEmpty(his_code.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入HIS编码");
                }else if(null != dj && StringUtils.isEmpty(dj.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入单价");
                }else if(!BinderUtil.isNumeric(dj.getText().toString())){
                    MyToastUtil.ShowToast(servicepack_item_add.this, "单价必须为有效数字！");
                }else if(null != cs && StringUtils.isEmpty(cs.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入次数");
                }else if(!BinderUtil.isNumeric(cs.getText().toString())){
                    MyToastUtil.ShowToast(servicepack_item_add.this, "单价必须为有效数字！");
                }else{
                    if (obj == null) {
                        obj = new ServicePackageItem();
                    }
                    obj.setItem_name(item_name.getText().toString());
                    obj.setHis_item_code(his_code.getText().toString());
                    obj.setTimes(cs.getText().toString());
                    obj.setDj(dj.getText().toString());

                    if (NetWorkUtils.isNetworkConnected(con)) {///网络连接
                        RequestParams ps = new RequestParams();
                        ps.add("entity.item_name", obj.getItem_name());
                        ps.add("entity.item_code", obj.getItem_code());
                        ps.add("entity.his_item_code", obj.getHis_item_code());
                        ps.add("entity.his_item_name", obj.getHis_item_name());
                        ps.add("entity.dj",obj.getDj());
                        ps.add("entity.times",obj.getTimes());
                        ps.add("entity.service_pack_id", getIntent().getStringExtra("servicepackid"));

                        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                        ps.add("type","2");

                        UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                                String content = new String(response);
                                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                                if (ret.getCode().equals("200")) {
                                    MyToastUtil.ShowToast(con, "成功");
                                }else{
                                    MyToastUtil.ShowToast(con, ret.getInfo());
                                }
                                finish();
                            }
                            @Override
                            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                                MyToastUtil.ShowToast(con, "数据传输失败");
                            }
                        });
                    } else {//网络没有链接
                        MyToastUtil.ShowToast(con, "请联网操作！");
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }
}
